﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.IO;
using System.Net;
using log4net.Core;
using log4net;
using DWH.Data;
using DWH.Utils;
using System.Drawing;

namespace DWH.PP.LoopUrls.ISTOR
{
    /// <summary>
    /// istorrs.com
    /// 
    /// </summary>
    public class ISTORPullTask : PPLoopUrlsBoardPullTask
    {
        private ILog logger;

        public ISTORPullTask()
        {
            logger = LogManager.GetLogger(typeof(ISTORPullTask));
        }

        public override string ProcessTargetHtmlContent(string pageUrl, string targetHtmlContent)
        {
            if (!String.IsNullOrEmpty(attachmentStoreFolder))
            {
                string attFolderForThisPage = Path.Combine(attachmentStoreFolder, pageUrl.GetHashCode().ToString());
                //if (!Directory.Exists(attFolderForThisPage))
                //{
                //    Directory.CreateDirectory(attFolderForThisPage);
                //}  
                //move download attachment to another task, skip the foder creation

                List<string> images = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent,
                    DWHConstants.REGEX_HTML_IMG, DWHConstants.REGEX_HTML_IMG_GROUP_IMGURL);

                DWHDBHelper.AddUndownloadedPageAttachments(images, pageUrl.GetHashCode(),
                    DWHConstants.ATTACHMENT_TYPE_IMAGE,
                    //"Image",
                    attFolderForThisPage, DateTime.Now);

                //DWHHelper.DownloadWebPageImages(TaskName, attFolderForThisPage, images);                
            }

            //<a href="http://www.new080.com/food/106">
            //http://www.new080.com/Food/106/links.txt

            string downloadLinks = null;

            try
            {
                Match m = Regex.Match(targetHtmlContent, @"http://www.new080.com/food/\d+", RegexOptions.IgnoreCase);
                if (m != null && !String.IsNullOrEmpty(m.Value))
                {
                    WebClient wc = new WebClient();
                    downloadLinks = wc.DownloadString(String.Format("{0}/{1}", m.Value, "links.txt"));
                    //downloadLinks = File.ReadAllText(String.Format("{0}/{1}", m.Value, "links.txt"));
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("task {0}, exception occures when trying to processing url {1} targetHtmlContent:{2}, exception:{3}", TaskName, pageUrl, targetHtmlContent, ex.ToString());
            }

            if (!String.IsNullOrEmpty(downloadLinks))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}<div>{1}</div>", targetHtmlContent, downloadLinks);

                return sb.ToString();
            }
            else
            {
                return base.ProcessTargetHtmlContent(pageUrl, targetHtmlContent);
            }
        }
    }
}
